use redis::RedisError;

use std::{fmt::Debug, io};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    #[error(transparent)]
    Io(#[from] io::Error),
    #[error(transparent)]
    Yaml(#[from] serde_yaml::Error),
    #[error(transparent)]
    MySql(#[from] sqlx::Error),
    #[error(transparent)]
    Clickhouse(#[from] clickhouse_rs::errors::Error),
    #[error("报表:{rept_name}配置出错，{msg}")]
    Conf {
        rept_name: String,
        msg: String,
    },
    #[error("创建报表:{rept_name}出错，{msg}")]
    Create {
        rept_name: String,
        msg: String,
    },
    #[error("读取报表:{rept_name}数据出错，{msg}")]
    Read {
        rept_name: String,
        msg: String,
    },
    #[error(transparent)]
    Redis(#[from] RedisError),
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}
impl Error {
    pub fn from_conf(
        rept_name: impl AsRef<str>,
        msg: impl AsRef<str>,
    ) -> Self {
        Error::Conf {
            rept_name: rept_name.as_ref().to_owned(),
            msg: msg.as_ref().to_owned(),
        }
    }
    pub fn from_create(
        rept_name: impl AsRef<str>,
        msg: impl AsRef<str>,
    ) -> Self {
        Error::Create {
            rept_name: rept_name.as_ref().to_owned(),
            msg: msg.as_ref().to_owned(),
        }
    }
    pub fn from_read(
        rept_name: impl AsRef<str>,
        msg: impl AsRef<str>,
    ) -> Self {
        Error::Read {
            rept_name: rept_name.as_ref().to_owned(),
            msg: msg.as_ref().to_owned(),
        }
    }
}
